<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" media="all" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
</head>
<body>
<!--信息表-->
<table id="role" lay-filter="roleTableFilter">
</table>
</body>
<!--头部工具栏按钮-->
<script type="text/html" id="roleToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-xs" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
    </div>
</script>
<!--操作按钮-->
<script type="text/html" id="roleOperation">
    <button class="layui-btn layui-btn-xs" lay-event="edit">修改</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>

<!--添加弹窗表格-->
<form class="layui-form" action="" id="addRoleForm" style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" placeholder="请输入角色名称" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色关键字</label>
        <div class="layui-input-block">
            <input type="text" name="roleKey" placeholder="请输入角色关键字" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="roleSort" placeholder="请输入排序" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest" lay-text="正常|失效">
        </div>
    </div>
    <input hidden name="createBy" th:value="${session.username}">
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入备注" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <!--树形菜单-->
    <div class="layui-form-item">
        <label class="layui-form-label" style="float: left">菜单权限</label>
        <div style="margin-left: 120px">
            <div id="addRoleTree" class="demo-tree-more"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-block">
            <input type="text" name="createBy" required lay-verify="required" placeholder="请输入创建人" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit id="addRoleButton" lay-filter="addRoleFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--修改弹窗表格-->
<form class="layui-form" action="" id="updateRoleForm" lay-filter="updateRoleFormFilter"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <input hidden name="roleId">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" placeholder="请输入角色名称" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色关键字</label>
        <div class="layui-input-block">
            <input type="text" name="roleKey" placeholder="请输入角色关键字" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="roleSort" placeholder="请输入排序" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" id="updateStatus" lay-skin="switch" lay-filter="switchTest"
                   lay-text="正常|失效">
        </div>
    </div>
    <%-- <input hidden name="updateBy" th:value="${session.username}">--%>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入备注" required lay-verify="required" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <!--树形菜单-->
    <div class="layui-form-item">
        <label class="layui-form-label" style="float: left">菜单权限</label>
        <div style="margin-left: 120px">
            <div id="updateRoleTree" class="demo-tree-more"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-block">
            <input type="text" name="updateBy" required lay-verify="required" placeholder="请输入修改人" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateRoleFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--状态按钮-->
<script type="text/html" id="roleStatus">
    {{# if (d.status === '0'){}}
    <button class="layui-btn layui-btn-xs" lay-event="state">正常</button>
    {{# }else {}}
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="state">失效</button>
    {{# }}}
</script>

  </body>
  <script>

      layui.use(['table', 'layer', 'jquery', 'form', 'tree', 'util'], function () {
          let table = layui.table;
          let layer = layui.layer;
          let $ = layui.jquery;
          let form = layui.form;
          let tree = layui.tree
          let util = layui.util
          //第一个实例
          let roleTable = table.render({
              elem: '#role'
              , url: '${pageContext.request.contextPath}/role/findAllRole.do' //数据接口
              , toolbar: '#roleToolbar' //开启头部工具栏，并为其绑定左侧模板
              , page: true //开启分页
              , cols: [
                  [ //表头
                      {field: 'roleId', title: '编号', width: 80, sort: true}
                      , {field: 'roleName', title: '名称', width: 100}
                      , {field: 'roleKey', title: '角色关键字', width: 120}
                      , {field: 'roleSort', title: '显示顺序', width: 160, sort: true}
                      , {field: 'status', title: '状态', width: 160, toolbar: "#roleStatus"}
                      , {field: 'createBy', title: '创建人', width: 100}
                      , {field: 'createTime', title: '创建时间', width: 160}
                      , {field: 'updateBy', title: '修改人', width: 100}
                      , {field: 'updateTime', title: '修改时间', width: 160}
                      //添加按钮
                      , {fixed: 'right', align: 'center', title: '操作', width: 220, toolbar: "#roleOperation"}
                  ]
              ]
          });

      //触发排序事件
      table.on('sort(test)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
        console.log(obj.field); //当前排序的字段名
        console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
        console.log(this); //当前排序的 th 对象

        //尽管我们的 table 自带排序功能，但并没有请求服务端。
        //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
        table.reload('demo', {
          initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
          ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
            field: obj.field //排序字段
            ,order: obj.type //排序方式
          }
        });
      });

      //模糊查询,监听提交
      form.on('submit(formDemo)', function(data){
        table.reload('demo',{
          where: {
            roleName: data.field.roleName
          }
          ,page: {
            curr: 1
          }
        });
        return false;
      });

      //重置按钮绑定单击事件
      $("#search_reset").click(function(){
        table.reload('demo',{
          where: {
            roleName: ""
          },
          page: {
            curr: 1
          }
        });
      });
          //头工具栏事件
          table.on('toolbar(roleTableFilter)', function (obj) {
              let checkStatus = table.checkStatus(obj.config.id)
                  , data = checkStatus.data;
              switch (obj.event) {
                  //添加数据
                  case 'add':
                      //重新渲染表单
                      form.render();
                      layer.open({
                          title: '添加角色',
                          type: 1,
                          area: ['500px', '500px'],
                          content: $("#addRoleForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                      });
                      break;
              }
          });
          //行工具栏事件
          table.on('tool(roleTableFilter)', function (obj) {
              let data = obj.data
              switch (obj.event) {
                  case 'edit':
                      console.log(data.status)
                      if (data.status == 0) {
                          document.getElementById("updateStatus").checked = true
                      } else {
                          document.getElementById("updateStatus").checked = false
                      }
                      form.val('updateRoleFormFilter', {
                          roleId: data.roleId
                          , roleName: data.roleName
                          , roleKey: data.roleKey
                          , roleSort: data.roleSort
                          , remark: data.remark
                      });
                      tree.reload('updateFormMenu')
                      //树形菜单回显
                      $.ajax({
                          url: "${pageContext.request.contextPath}/role/findSelectMenu.do",
                          type: "GET",
                          dataType: "JSON",
                          data: {roleId: data.roleId},
                          success: function (ret) {
                              tree.setChecked('updateFormMenu', ret);
                          }
                      })
                      form.render()

                      layer.open({
                          title: '修改角色',
                          type: 1,
                          area: ['500px', '500px'],
                          content: $("#updateRoleForm"),//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                      });
                      break;
                  //删除该行
                  case 'del':
                      layer.confirm('真的删除行么', function (index) {
                          $.ajax({
                              url: "${pageContext.request.contextPath}/role/deleteRole.do",
                              type: "POST",
                              dataType: "JSON",
                              data: {roleId: data.roleId},
                              success: function (result) {
                                  if (result.code === 0) {
                                      roleTable.reload();
                                      layer.msg(result.msg, {icon: 6});
                                  } else {
                                      layer.msg(result.msg, {icon: 5});
                                  }
                                  layer.closeAll();//关闭对话框
                              }
                          })
                          layer.close(index);
                      });
                      break;
              }
          });

          //获取选中的菜单id
          function getChecked_list(data) {
              let id = '';
              $.each(data, function (index, item) {
                  if (id !== "") {
                      id = id + "," + item.id;
                  } else {
                      id = item.id;
                  }
                  let i = getChecked_list(item.children);
                  if (i !== "") {
                      id = id + "," + i;
                  }
              })
              return id;
          }
          //addUserForm表单的提交
          form.on('submit(addRoleFilter)', function (data) {
              $("#addRoleButton").attr("class","layui-btn layui-btn-disabled")
              console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
              let checkData = tree.getChecked('addFormMenu');
              let list = getChecked_list(checkData)
              data.field.menus = list;
              //将状态属性的on转换为0，1
              if (data.field.status === "on") {
                  data.field.status = 0;
              } else {
                  data.field.status = 1;
              }
              //提交表单数据
              $.ajax({
                  url: "${pageContext.request.contextPath}/role/addAllRole.do",
                  type: "POST",
                  dataType: "JSON",
                  data: data.field,
                  success: function (ret) {
                      if (ret.code == 0) {
                          //关闭对话框
                          layer.closeAll();
                          //刷新表格数据
                          roleTable.reload({
                              page: {
                                  curr: 1 //重新从第 1 页开始
                              }
                          });
                          layer.msg(ret.msg, {icon: 6});
                      } else {
                          layer.msg(ret.msg, {icon: 5});
                      }
                  }
              })
              //将表单清空
              document.getElementById("addRoleForm").reset();
              return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
          });
          //update表单的提交
          form.on('submit(updateRoleFilter)', function (data) {
                  console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                  let checkData = tree.getChecked('updateFormMenu');
                  let list = getChecked_list(checkData)
                  data.field.menus = list;
                  //将状态属性的on转换为0，1
                  if (data.field.status === "on") {
                      data.field.status = 0;
                  } else {
                      data.field.status = 1;
                  }
                  //提交表单数据
                  $.ajax({
                      url: "${pageContext.request.contextPath}/role/updateRole.do",
                      type: "POST",
                      dataType: "JSON",
                      data: data.field,
                      success: function (ret) {
                          if (ret.code == 0) {
                              //关闭对话框
                              layer.closeAll();
                              //刷新表格数据
                              roleTable.reload({});
                              layer.msg(ret.msg, {icon: 6});
                          } else {
                              layer.msg(ret.msg, {icon: 5});
                          }
                      }
                  })
                  //将表单清空
                  document.getElementById("updateRoleForm").reset();
                  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
              }
          );

          //从后台查询菜单树格式后的数据，回显在表单中
          $.ajax({
              url: "${pageContext.request.contextPath}/role/selectAllMenu.do",
              type: "GET",
              dataType: "JSON",
              success: function (list) {
                  //常规用法
                  tree.render({
                      elem: '#addRoleTree' //默认是点击节点可进行收缩
                      , data: list
                      , showCheckbox: true
                      , id: 'addFormMenu'
                  });

                  tree.render({
                      elem: '#updateRoleTree' //默认是点击节点可进行收缩
                      , data: list
                      , showCheckbox: true
                      , id: 'updateFormMenu'
                  });
              }
          })
    });
  </script>
</html>

